<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
%>
<jsp:include page="../../import.jsp" flush="true" />
<link rel="StyleSheet" href="<%=path%>/lib/dtree/dtree.css" type="text/css" />
<script type="text/javascript" src="<%=path%>/lib/dtree/dtree.js"></script>
<SCRIPT LANGUAGE="JavaScript">

$(function(){
	$("#editDiv").dialog({
		autoOpen: false,
		bgiframe: true,
		height: 500,
		width:350,
		modal: true,
		buttons: {
			"提交": function() { 
				//$(this).dialog("close"); 
				_submit();
			}, 
			"取消": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
		$("#waiting").dialog({
		autoOpen: false,
		bgiframe: true,
	//	height: 300,
		modal: true
	});
});

var menuCache={};

</SCRIPT>
<body>

<TABLE width="99%">
<TR>
	<TD width="50%">
		<div class="dtree">
			<script type="text/javascript">
				<!--
				mytree = new dTree('mytree');
			 <s:iterator value="rb.resultList" id="m" status="cust_stat">    
				menuCache['<s:property value="ID"/>'] = {'id':<s:property value="ID"/>,'pid':<s:property value="PID"/>,'name':'<s:property value="MENUNAME"/>'};
				 mytree.add(<s:property value="ID"/>,<s:property value="PID"/>,
				'<s:property value="MENUNAME"/>'+
				'[<A HREF="javascript:editNode(\'<s:property escape="false"/>\')" class="edit">修改</A>]'+
				<s:if test="#m.PID!=-1">'[<A HREF="javascript:delNode(<s:property value="ID" />,\'<s:property value="MENUNAME"/>\');"  class="edit">删除</A>]' +</s:if>
				'[<A HREF="javascript:addNode(<s:property value="ID" />);"  class="edit" title="增加子目录">增加</A>][<A class="edit" HREF="javascript:setOrder(<s:property value="ID" />)">排序</A>]'
				//,'#'
				);	
			</s:iterator> 
				document.write(mytree);
				//-->
			</script>
		</div>
	</TD>
	<TD valign="top">
		<div id="setOrderDiv" style="display:none">
				<div id="selectDiv"></div>
				<div><input name="" type="button" value="∧" onclick="upPosition()"><input name="" type="button" value="∨" onclick="downPosition()"></div>
				<div><input type="button" value="设定此排序" onclick="uploadOrder();"></div>
		</div>
	</TD>
</TR>
</TABLE>
	


<div id="waiting" title="数据正在提交，请稍等... ..." style="display:none">
	<p align='center'><br>
		<IMG SRC="<%=path%>/css/05043110.gif">
	</p>
</div>
<div id="editDiv" title="编辑菜单项" >
	<span align='center'>
		<FORM METHOD='POST' ACTION='<%=path%>/menu/menuUpdate.action' onsubmit="return checkData();">
			<INPUT TYPE="hidden" NAME="pid"  id="pid" value=''>
			<INPUT TYPE="hidden" NAME="id"   id="id" value=''>
			<INPUT TYPE="hidden" NAME="oper"   id="oper" value=''>
			<INPUT TYPE="hidden" NAME="index"   id="index" value=''>
			<INPUT TYPE="hidden" NAME="param"   id="param" value=''>
			<TABLE >
				<TR>
					<TD >名称：</TD>
					<TD><INPUT TYPE="text" NAME="menuName" id="menuName"  value=''></TD>
				</TR>
					<TR>
					<TD>图标连接：</TD>
					<TD><INPUT TYPE="text" NAME="icon" id="icon"  value=''></TD>
				</TR>
				<TR>
					<TD>连接地址：</TD>
					<TD><INPUT TYPE="text" NAME="linkurl" id="linkurl"  value=''></TD>
				</TR>
				<TR>
					<TD>目标：</TD>
					<TD><INPUT TYPE="text" NAME="target" id="target"  value=''></TD>
				</TR>
			</TABLE>
		</FORM>
		<div>
			
		</div>
	</span>
</div>

<!-- <TEXTAREA id="debug" ROWS="50" COLS="100"></TEXTAREA> -->
</body>
<SCRIPT LANGUAGE="JavaScript">


var onclickNode  = function(id){
	alert(id);
}

var checkData = function(){
	return true;
}

var objectSelect;
var setOrder = function(id){
	$('#setOrderDiv').hide();
	var tmp = "<select id='orderSelectContainer' size='10'>";
	var o;
	for(each in menuCache){
		o = menuCache[each];
		if(o.pid == id){
			tmp += "<option value='"+o.id+"'>"+o.name+"</option>"
		}
	}
	tmp +="</select>"

	if(tmp.length<60)return;

	$('#setOrderDiv').show();
	$('#selectDiv').html(tmp);
	objectSelect = document.all.orderSelectContainer;
}

var uploadOrder = function(){
		var tmp = [];
		for(i=0;i<objectSelect.options.length;i++){
			tmp.push(i+','+objectSelect.options[i].value); 
		}
		var act = $('form').attr('action');
		$('form').attr('action',act.replace('Update','UpdateBat'));
		$('#param').val(tmp.join(';'));
		$('#index').val("menuOrderUpdate");
		$('#waiting').dialog('open');
		$('form:first').submit();
}
 
var _submit = function(){
	
	if($('#menuName').val()==''){
		alert("导航名称必须输入！");
	}else{
		$('#waiting').dialog('open');
		$('form:first').submit();
	}
}

var editNode = function(_menu){	
	if(_menu==null){
		return;
	}
	var temp = _menu.replace(/{|}| /g,'').split(',');//去杂，分组
	var tmpData = {};
	for(var i=0;i<temp.length;i++){
		if(temp[i].indexOf('=')>-1){
			tmpData[temp[i].substring(0,temp[i].indexOf('='))] = temp[i].substring(temp[i].indexOf('=')+1);
		}
	}

	$('#editDiv').dialog('open');
	$('#id').val(tmpData.ID);
	$('#pid').val(tmpData.PID);
	$('#oper').val(tmpData.OPER);
	$('#menuName').val(tmpData["MENUNAME"]);
	$('#linkurl').val(tmpData.LINKURL);
	$('#target').val(tmpData.TARGET);
	$('#icon').val(tmpData.ICON);
	$('#index').val('menu_update');
//	alert(Log.debug(menu));

}

var delNode = function(id,name){
	if(confirm('确定删除【'+name+'】?')){
		$('#id').val(id);
		$('#menuName').val('delete');
		//$('form:first').attr('action','menu!delete.action');
		$('#index').val('menu_delete');
		_submit();
	}
}
var addNode = function(id){
	$('#editDiv').attr("title","增加新菜单").dialog('open');
	$('input').val('');
	$('#pid').val(id);
	$('#oper').val('-1');
	$('#index').val('menu_insert');
}

if(top.reLoadMenu){
	top.reLoadMenu();
}

function upPosition(){
	var selectPoint = 0;
	for(i=0;i<objectSelect.options.length;i++){
		if(objectSelect.options[i].selected==true){
			selectPoint = i;
		}
	}
	if(selectPoint>0){
		upKey = objectSelect.options[selectPoint].text;
		upValue = objectSelect.options[selectPoint].value;
		objectSelect.options[selectPoint] = new Option(objectSelect.options[(selectPoint-1)].text,objectSelect.options[(selectPoint-1)].value);
		objectSelect.options[selectPoint-1] = new Option(upKey,upValue);
		objectSelect.options[selectPoint-1].selected=true;
	}
 }
 function downPosition(){
	var selectPoint = 0;
	for(i=0;i<objectSelect.options.length;i++){
		if(objectSelect.options[i].selected==true){
			selectPoint = i;
		}
	}
	if(selectPoint<objectSelect.options.length){
		downKey = objectSelect.options[selectPoint].text;
		downValue = objectSelect.options[selectPoint].value;
		objectSelect.options[selectPoint] = new Option(objectSelect.options[(selectPoint+1)].text,objectSelect.options[(selectPoint+1)].value);
		objectSelect.options[selectPoint+1] = new Option(downKey,downValue);
		objectSelect.options[selectPoint+1].selected=true;
	}
 } 

//$("#debug").val($('.dtree').html());
</SCRIPT>